package com.founder.bm.util;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;

import org.apache.tools.zip.ZipEntry;
import org.apache.tools.zip.ZipOutputStream;

public class ZipUtil {
	public String fullPath;
	public File inpath;
	public ZipOutputStream zout = null; //输出流确定
	
	public ZipUtil(String path) {
		fullPath = FileOperate.getDir("saveDir") + "\\excel\\" + path;
		inpath = new File(fullPath);
		try {
			zout = new ZipOutputStream(new FileOutputStream(fullPath + ".zip"));
			zout.setEncoding("GBK");
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		}
	}

	public String dozip() {
		File file = new File(fullPath);
		File[] files = file.listFiles();
		for (File f : files) {
			if (f.isFile()) {
				docopy(f);
			}
		}
		return fullPath + ".zip";
	}

	public void docopy(File f) {
		try {
			InputStream in = new FileInputStream(f);
			//创建压缩条目
			String tempPath = f.getAbsolutePath().replace(
					inpath.getAbsolutePath() + "\\", "");
			ZipEntry zent = new ZipEntry(tempPath);
			zout.putNextEntry(zent);
			byte[] buff = new byte[1024];
			int len = 0;
			while ((len = in.read(buff)) != -1) {
				zout.write(buff, 0, len);
			}
			in.close();

		} catch (FileNotFoundException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
	}
}
